package com.tchy.cloud.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tchy.cloud.entity.CQDimEntity;
import com.tchy.cloud.entity.CQQOptionEntity;
import com.tchy.cloud.entity.CQuesDimResultEntity;
import com.tchy.cloud.entity.CQuestionnaireEntity;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.vendor.OpenJpaDialect;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户测评结果分析
 * xmweilove
 */
@Controller
@RequestMapping("/cqDimResultController")
public class CQDimResultController extends BaseController {
    private static final Logger logger = Logger.getLogger(CQDimResultController.class);

    @Autowired
    private CommonService commonService;

    /**
     * 用户测评结果列表展示页面跳转
     *
     * @return
     */
    @RequestMapping(params = "index")
    public ModelAndView index(HttpServletRequest request) {
        String cqid = request.getParameter("cqid");
        request.setAttribute("cqid", cqid);
        return new ModelAndView("clound/cqdimResult/index");
    }


    @RequestMapping(params = "getData")
    @ResponseBody
    public List<Map<String, Object>> getData(HttpServletRequest request) {
        String cqid = request.getParameter("cqid");
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(" select t3.dim_name,t2.result_name,REPLACE(t2.result_desc,'{X}',t1.old_score) result_desc,t1.old_score from c_ques_dim_result t1");
            sb.append(" INNER JOIN c_ques_score_result t2 on t1.ques_score_result_id = t2.ques_score_result_id");
            sb.append(" INNER JOIN c_ques_dim t3 on t1.ques_dim_id = t3.ques_dim_id");
            sb.append("  where t1.consumer_ques_id = ?");
            list = commonService.findForJdbc(sb.toString(), new Object[]{Long.valueOf(cqid)});
        } catch (Exception e) {
            logger.error(e);
        }

        return list;
    }


    /**
     * 用户测评明细 列表展示页面跳转
     *
     * @return
     */
    @RequestMapping(params = "index_detail")
    public ModelAndView index_detail(HttpServletRequest request) {
        String cqid = request.getParameter("cqid");
        request.setAttribute("cqid", cqid);
        return new ModelAndView("clound/cqdimResult/index_detail");
    }


    @RequestMapping(params = "getData_detail")
    @ResponseBody
    public List<Map<String, Object>> getData_detail(HttpServletRequest request) {
        String cqid = request.getParameter("cqid");
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(" select");
            sb.append(" '回答',t1.consumer_ques_id,t1.questionnaire_id,t1.submit_time,");
            sb.append(" '问题',t3.ques_ques_id,t3.name,t3.ques_type,t4.typename,t3.question_no,");
            sb.append("  '选项',t2.ques_ques_option_id,t2. answer_value");
            sb.append(" from  c_consumer_ques t1");
            sb.append(" INNER JOIN c_consumer_answer t2 on t2.consumer_ques_id = t1.consumer_ques_id");
            sb.append(" inner JOIN c_ques_ques t3 on t3.ques_ques_id = t2.ques_ques_id");
            sb.append(" inner join t_s_type t4 on t4.typecode = t3.ques_type and t4.typegroupid = 'ff8080815b61c14d015b61c60828001c'");
            sb.append(" where t1.consumer_ques_id = ?");
            sb.append(" order by t3.question_no");
            list = commonService.findForJdbc(sb.toString(), new Object[]{Long.valueOf(cqid)});


            for (int i = 0; i < list.size(); i++) {
                List<Map<String, Object>> ques_options_list = new ArrayList<>();
                Map<String, Object> obj = list.get(i);
                String[] ques_ques_option_id_s = String.valueOf(obj.get("ques_ques_option_id")).split(",");//某个问题的被勾中 选项
                JSONArray answer_value_s = JSONObject.parseArray(String.valueOf(obj.get("answer_value")));//选项对应的回答

                for (int m = 0; m < ques_ques_option_id_s.length; m++) {
                    Integer qq_option_id = Integer.valueOf(ques_ques_option_id_s[m]);//选项id
                    String option_sql = "select ques_ques_option_id,option_identity,name,score from c_ques_ques_option where ques_ques_option_id = ?";
                    Map<String, Object> option = commonService.findOneForJdbc(option_sql, new Object[]{qq_option_id});//选项

                    if (answer_value_s.size() > 0) {
                        for (int n = 0; n < answer_value_s.size(); n++) {
                            Map<String, String> qq_option_obj = (Map<String, String>) answer_value_s.get(n);
                            Integer qq_option_id_temp = Integer.valueOf(qq_option_obj.get("optionId"));//选项id
                            String qq_option_answer_temp = qq_option_obj.get("answer");//选项的回答
                            if (qq_option_id == qq_option_id_temp) {
                                option.put("answer", qq_option_answer_temp);
                                break;
                            }
                        }
                    } else {
                        option.put("answer", "");
                    }
                    ques_options_list.add(option);
                    obj.put("option_list", ques_options_list);
                }
            }
        } catch (Exception e) {
            logger.error(e);
        }

        return list;
    }
}
